/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：ProgressScreen.java <br>
 * 创建时间：2013-8-30 上午11:45:23 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.core;

import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;

/**
 * 类名：ProgressScreen <br>
 * 说明：
 *
 * @author 12548
 */
public abstract class ProgressScreen extends SlowScreen {

	protected volatile boolean ok = true;

	int speed;
	protected int max;
	protected int now = 0;
	protected int smax;

	public ProgressScreen(float ticktime, int speed, int max, int smax) {
		super(ticktime);
		this.speed = speed;
		this.max = max;
		this.smax = smax;
		ok = true;
	}

	public ProgressScreen(float ticktime, int speed, int max, int now, int smax) {
		super(ticktime);
		this.speed = speed;
		this.max = max;
		this.now = now;
		this.smax = smax;
		ok = true;
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.SlowScreen#tick()
	 */
	@Override
	public void tick() {
//		if(!ok)return;
		int multiplier = 1;
		if (speed < 1) {
			speed = 1;
		}

		while ((speed * multiplier * 20) < max) {
			multiplier++;
		}

		tick(multiplier);
	}


	public void tick(int multiplier) {
		if (speed < 1) {
			speed = 1;
		}
		now += speed * multiplier;
		binding(now);
		if (now >= max) {
			onComplete();
		}
	}


	public abstract void onComplete();

	protected abstract void binding(int now);


	@Override
	public void present(float t) {
		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		if (max <= 0) return;
		g.drawRect(0, 2, 320, 16, Gmud.getBgColor());
		g.drawRectOutline(30, 2, 200, 16, 0);
		g.drawText(now + "/" + smax, 236, 3, FontSize.FT_12PX);

		final int x = 30, y = 2, height = 16;
		final int width = Math.min((int) (now * 1.0 / max * 200 + 30), 200);
		g.drawRect(x, y, width, height, 0);

	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.SlowScreen#onClick(int, int)
	 */
	@Override
	protected void onClick(int tx, int ty) {
//		game.setScreen(bg);
		getGame().popScreen();
	}


}
